// 请求通用方法
import apiMixin from '@/mixin/apiMixin';

import baseConfig from '@/config/env.js';

const checkPrice = (rule, value, callback) => {
    if (!value) {
        return callback(new Error('该项为必填项'));
    }
    if (value < 0.1) {
        callback(new Error('该项值必须大于0.1'));
    }
    callback();
}
export default {
    mixins: [apiMixin],
    data() {
        return {
            loading: true,
            configObj: {
                platform_name: '',
                minRecharge: '',
                minCash: '',
                maxCash: '',
                cashCount: '',
                maxTopLimit: '',
                minAutoPayment: '',
                photoSize: '',
                task_deadline_time: '',
                reviewCaseTime: '',
                task_limited_time_approve: '',
                inviteInstroduce: '',
                indexLayer: '',
                receivablesCount: '',
                amount_platform_ratio: '',
                cashService: '',
                topHourFee: '',
                fatherAcceptCommission: '',
                grandpaAcceptCommission: '',
                pushArbitrationBad: '',
                acceptTaskOverTime: '',
                acceptTaskError: '',
                pushTaskSuccess: '',
                acceptTaskSuccess: '',
                firstphaseTaskcount: '',
                secondphaseTaskcount: '',
                thirdphaseTaskcount: '',
                difficultyAcceptCount: 0,
                reward2Money: 0,
                reward3Money: 0,
                rewardRule: "",
                pictureCheck: "",
                seniorTaskRewardCount: "",
                rewardeMoney: "",
                subordinateFinishTaskCount: 0, // 新被邀请用户完成奖励任务数
                inviteCopyTitle: '', // 邀请页标题 
                inviteCopyContent: '' //邀请页内容
            },
            rules: {
                platform_name: [
                    {
                        required: true, message: '请输入平台名称', trigger: 'blur',
                    },
                ],
                pictureCheck: [
                    {
                        required: true,
                        trigger: 'blur',
                    },
                ],
                seniorTaskRewardCount: [
                    {
                        required: true,
                        trigger: 'blur',
                    },
                ],
                rewardeMoney: [
                    {
                        required: true,
                        trigger: 'blur',
                    },
                ],
                subordinateFinishTaskCount: [
                    {
                        required: true,
                        message: '该项为必填项',
                        trigger: 'blur',
                    }
                ],
                inviteCopyTitle: [
                    {
                        required: true,
                        message: '该项为必填项',
                        trigger: 'blur',
                    }
                ],
                inviteCopyContent: [
                    {
                        required: true,
                        message: '该项为必填项',
                        trigger: 'blur',
                    }
                ],
                minRecharge: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                minCash: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                maxCash: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                cashCount: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                maxTopLimit: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                minAutoPayment: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                photoSize: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                task_deadline_time: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                reviewCaseTime: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                task_limited_time_approve: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                task_limited_time: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                inviteInstroduce: [
                    {
                        required: true, message: '请输入推广规则', trigger: 'blur',
                    },
                ],
                indexLayer: [
                    {
                        required: true, message: '请输入首页弹窗公告', trigger: 'blur',
                    },
                ],
                receivablesCount: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                amount_platform_ratio: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                cashService: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                topHourFee: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                fatherAcceptCommission: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                grandpaAcceptCommission: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                pushArbitrationBad: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                acceptTaskOverTime: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                acceptTaskError: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                pushTaskSuccess: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                acceptTaskSuccess: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                // firstphaseTaskcount: [
                //     {
                //         required: true, message: '该项为必填项', trigger: 'blur',
                //     },
                // ],
                // secondphaseTaskcount:  [
                //     {
                //         required: true, message: '该项为必填项', trigger: 'blur',
                //     },
                // ],
                // thirdphaseTaskcount: [
                //     {
                //         required: true, message: '该项为必填项', trigger: 'blur',
                //     },
                // ],
                // firstphaseTaskreward: [
                //     {
                //         required: true, message: '该项为必填项', trigger: 'blur',
                //     },
                // ],
                // secondphaseTaskreward: [
                //     {
                //         required: true, message: '该项为必填项', trigger: 'blur',
                //     },
                // ],
                // thirdphaseTaskreward: [
                //     {
                //         required: true, message: '该项为必填项', trigger: 'blur',
                //     },
                // ],
                difficultyAcceptCount: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                reward1Money: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                reward2Money: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                reward3Money: [
                    {
                        required: true, message: '该项为必填项', trigger: 'blur',
                    },
                ],
                rewardRule: [
                    {
                        required: true, message: '请输入奖励规则', trigger: 'blur',
                    },
                ],
                sysrangeReward: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                lowestUsermoney: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                subordinateFirstTask: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                subordinateFirstLoginApp: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
                subordinateFirstDrawn: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkPrice
                    },
                ],
            }
        }
    },
    mounted() {
        this.getSystemConfigApi();
    },
    methods: {
        getSystemConfigApi() {
            let _self = this;
            this.comResquest(this.request.getSystemConfig, (res) => {
                let { result } = res;
                // 金额转换
                result.minRecharge = _self.transformPrice(result.minRecharge, 1);
                result.minCash = _self.transformPrice(result.minCash, 1);
                result.maxCash = _self.transformPrice(result.maxCash, 1);
                result.minAutoPayment = _self.transformPrice(result.minAutoPayment, 1);
                result.topHourFee = _self.transformPrice(result.topHourFee, 1);
                result.substationPrice = _self.transformPrice(result.substationPrice, 1);
                result.firstphaseTaskreward = _self.transformPrice(result.firstphaseTaskreward, 1);
                result.secondphaseTaskreward = _self.transformPrice(result.secondphaseTaskreward, 1);
                result.thirdphaseTaskreward = _self.transformPrice(result.thirdphaseTaskreward, 1);
                //result.sysrangeReward = _self.transformPrice(result.sysrangeReward ,1);
                result.lowestUsermoney = _self.transformPrice(result.lowestUsermoney, 1);
                result.subordinateFirstTask = _self.transformPrice(result.subordinateFirstTask, 1);
                result.subordinateFirstLoginApp = _self.transformPrice(result.subordinateFirstLoginApp, 1);
                result.subordinateFirstDrawn = _self.transformPrice(result.subordinateFirstDrawn, 1);
                result.subordinateFinishTaskCount = result.subordinateFinishTaskCount;
                result.inviteCopyTitle = result.inviteCopyTitle;
                result.inviteCopyContent = result.inviteCopyContent;
                _self.configObj = result;
            }, {
                load: true,
            });
        },
        submitForm(formName) {
            let _self = this;
            let isPass = true;
            console.log(this.$refs[formName]);
            this.$refs[formName].validate((valid) => {
                if (valid) {
                } else {
                    console.log('error submit!!');
                    isPass = false;
                    return false;
                }
            });
            if (isPass) {
                this.editSystemConfigApi();
            }
        },
        editSystemConfigApi() {
            let resPar = JSON.parse(JSON.stringify(this.configObj));
            resPar.minRecharge = this.transformPrice(resPar.minRecharge, 0);
            resPar.minCash = this.transformPrice(resPar.minCash, 0);
            resPar.maxCash = this.transformPrice(resPar.maxCash, 0);
            resPar.minAutoPayment = this.transformPrice(resPar.minAutoPayment, 0);
            resPar.topHourFee = this.transformPrice(resPar.topHourFee, 0);
            resPar.substationPrice = this.transformPrice(resPar.substationPrice, 0);
            //resPar.firstphaseTaskreward = this.transformPrice(resPar.firstphaseTaskreward ,0);
            //resPar.secondphaseTaskreward = this.transformPrice(resPar.secondphaseTaskreward ,0);
            //resPar.thirdphaseTaskreward = this.transformPrice(resPar.thirdphaseTaskreward ,0);
            //result.sysrangeReward = _self.transformPrice(result.sysrangeReward ,1);
            resPar.lowestUsermoney = this.transformPrice(resPar.lowestUsermoney, 0);
            resPar.subordinateFirstTask = this.transformPrice(resPar.subordinateFirstTask, 0);
            resPar.subordinateFirstLoginApp = this.transformPrice(resPar.subordinateFirstLoginApp, 0);
            resPar.subordinateFirstDrawn = this.transformPrice(resPar.subordinateFirstDrawn, 0);
            resPar.subordinateFinishTaskCount = resPar.subordinateFinishTaskCount;
            resPar.inviteCopyTitle = resPar.inviteCopyTitle;
            resPar.inviteCopyContent = resPar.inviteCopyContent;
            let _self = this;
            this.comResquest(this.request.editSystemConfig, (res) => {
                _self.$message.success("修改成功！");
            }, resPar);
        },
        /**
         * 
         * @param { String } type 类型 1 转换成实际金额 0 转换成平台金额 
         */
        transformPrice(price, type) {
            let priceRadius = baseConfig.PRICE_RADIUS;
            if (type == 1) {
                return parseFloat(Number(price / priceRadius).toFixed(2));
            }
            return price * priceRadius
        }
    },
}